import QtQuick 2.0
import QtQuick.Window 2.15


import QtQuick.Controls 1.4
import QtQml.Models 2.15


Item {
    width: 800
    height: 480
    visible: true


    Rectangle{
        anchors.fill: parent
        anchors.margins: 20


        Row{
            id: idRowButton
            x:0
            y:0
            width: parent.width
            height: 30
            Button{
                id:idBtnAdd
                width: 100
                height: 30
                text: "导入"
                onClicked: {
                    slots.importfile()
                    //var jsonData = {"number":1, "team":"江苏队", "base":100, "scope":0, "total":100, "operate":"操作"}
                    //idListModel.append(jsonData)
                }
            }
            Button{
                id:idBtnAdd
                width: 100
                height: 30
                text: "添加"
                onClicked: {
                    var jsonData = {"number":1, "team":"江苏队", "base":100, "scope":0, "total":100, "operate":"操作"}
                    idListModel.append(jsonData)
                }
            }
            Button{
                id:idBtnDel
                width: 100
                height: 30
                text: "删除"
                onClicked: {
                    //删除当前选中行
                    idListModel.remove(idTabView.currentRow)
                }
            }
            Button{
                id:idBtnClear
                width: 100
                height: 30
                text: "清除"
                onClicked: {
                    idListModel.clear()
                }
            }
            Button{
                id:idBtnRead
                width: 140
                height: 30
                text: "读取选中行数据"
                onClicked: {
                    var currentIndex = idTabView.currentRow
                    if(currentIndex > -1) {
                        var obj = idListModel.get(currentIndex)
                        console.log(JSON.stringify(obj))
                    }
                }
            }
            Button{
                id:idBtnModify
                width: 100
                height: 30
                text: "修改第一行数据"
                onClicked: {
                    var jsonData = {"number":4, "team":"北京队", "base":100, "scope":0, "total":100}
                    idListModel.set(0, jsonData)
                }
            }
            Button{
                id:idBtnMoveUp
                width: 80
                height: 30
                text: "上移"
                onClicked: {
                    var curIndex = idTabView.currentRow
                    if(curIndex>0) {
                         idListModel.move(curIndex,curIndex-1,1)
                    }
                }
            }
            Button{
                id:idBtnMoveDown
                width: 80
                height: 30
                text: "下移"
                onClicked: {
                    var curIndex = idTabView.currentRow
                    if(curIndex>-1 && curIndex < idTabView.rowCount) {
                         idListModel.move(curIndex,curIndex+1,1)
                    }
                }
            }
        }


        TableView{
            id: idTabView
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: idRowButton.bottom
            anchors.bottom: parent.bottom
            anchors.topMargin: 20
            focus: true
            //表头名称设置
            TableViewColumn{ role: "number"; title:"序列号"; width:80; movable: false}
            TableViewColumn{ role: "team"; title:"队伍"; width:200; movable: false}
            TableViewColumn{ role: "base"; title:"基础分"; width:80; movable: false}
            TableViewColumn{ role: "scope"; title:"净得分"; width:80; movable: false}
            TableViewColumn{ role: "total"; title:"总分"; width:80; movable: false}


            // 设置表头的样式
            headerDelegate: Rectangle {
               implicitWidth: 10
               implicitHeight: 60
               border.width: 1
               border.color: "#838B8B"
               color: "#4F4F4F"
               Text {
                   anchors.centerIn: parent
                   text: styleData.value
                   color:"white"
               }
            }
            // 设置行的背景色
            rowDelegate: Rectangle {
                height: 48
                color: styleData.selected ? "red": "gray"
                border.width: 1
                border.color: "black"
            }
            model: ListModel{
                id: idListModel
                //初始化两行数据 也可以不初始化
                ListElement{
                    number:1;team:"浙江1队";base:100;scope:0;total:100;
                }
                ListElement{
                    number:2;team:"浙江2队";base:100;scope:0;total:100;
                }
            }
        }
    }
}